package cn.com.dhc.danlu.business.shopcar.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.LeadingMarginSpan;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.business.shopcar.bean.WrapDealerAttr;
import cn.com.dhc.danlu.util.CommonUtils;

/**
 * author: 吴海洋(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2016-12-03 9:43<br/>
 *
 * <p>
 * 未满足经销商最低起售金额弹出提示框
 * </p>
 */
public class BottomSheetTipsDialog extends Dialog implements ViewTreeObserver.OnGlobalLayoutListener {
    private View mRootView;

    public BottomSheetTipsDialog(Context context) {
        super(context, R.style.dialog_style);
        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        // 布局中设置高度min=200 dp
        mRootView = LayoutInflater.from(context).inflate(R.layout.dl_dealer_minamount_dialog, decorView, false);
        setContentView(mRootView);
        mRootView.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    public void refreshContent(@NonNull List<WrapDealerAttr> datas) {
        TextView tvContent = (TextView) mRootView.findViewById(R.id.tv_content);
        List<String> stringList = new ArrayList<>();
        String string = getContext().getResources().getString(R.string.dealer_minamount_tips);
        for (WrapDealerAttr model : datas) {
            String yuan = CommonUtils.fenToYuan(new BigDecimal(model.getMinAmount())).toPlainString();
            stringList.add(String.format(string, model.getSellerName(), yuan));
        }
        tvContent.setText(getOrderListSpan(stringList));
    }

    private SpannableStringBuilder getOrderListSpan(List<String> list) {
        SpannableStringBuilder msp = new SpannableStringBuilder();
        for (int i = 0; i < list.size(); i++) {
            int contentStart = msp.length();
            msp.append(list.get(i)).append("\n");
            msp.setSpan(new LeadingMarginSpan() {
                @Override
                public int getLeadingMargin(boolean first) {
                    return !first ? CommonUtils.dipToPx(getContext(), 15) : 0;
                }

                @Override
                public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {

                }
            }, contentStart, msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return msp;
    }

    @Override
    public void onGlobalLayout() {
        try {
            int measuredHeight = mRootView.getMeasuredHeight();
            WindowManager m = getWindow().getWindowManager();
            Display d = m.getDefaultDisplay();
            if (measuredHeight > d.getHeight() / 2) {
                // 对话框最大高度不能超过屏幕一半
                ViewGroup.LayoutParams params = mRootView.getLayoutParams();
                params.height = d.getHeight() / 2;
                mRootView.setLayoutParams(params);
                mRootView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        } catch (Exception ignored) {
            // no op..
        }
    }

    @SuppressLint("RtlHardcoded")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
            WindowManager.LayoutParams p = dialogWindow.getAttributes();
            WindowManager m = getWindow().getWindowManager();
            Display d = m.getDefaultDisplay();
            p.width = d.getWidth(); //设置dialog的宽度为当前手机屏幕的宽度
            dialogWindow.setAttributes(p);
        }
    }

    @Override
    public void dismiss() {
        mRootView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        super.dismiss();
    }

}
